home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_9 / rks4.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  727 b   |  27 lines  |  [MATF/MATL]

  1. function [T,Z] = rks4(Fn,a,b,Za,m)
  2. % [T,Y] = rks4(f,a,b,ya,m)
  3. % Runge-Kutta solution for the system
  4. % of DE's   Z' = F(t,Z) with Z(a) = Za.
  5. % f  is the function, input.
  6. % a  is the left endpoint, input.
  7. % b  is the right endpoint, input.
  8. % Za is the initial condition vector, input.
  9. % m  is the number of steps, input.
  10. % T  is the parameter vector, output.
  11. % Z  is the matrix of the vector solutions, output.
  12. h = (b - a)/m;
  13. T = zeros(1,m+1);
  14. Z = zeros(m+1,length(Za));
  15. T(1) = a;
  16. Z(1,:) = Za;
  17. for j=1:m,
  18.   tj = T(j);
  19.   Zj = Z(j,:);
  20.   K1 = h*feval(Fn,tj,Zj);
  21.   K2 = h*feval(Fn,tj+h/2,Zj+K1/2);
  22.   K3 = h*feval(Fn,tj+h/2,Zj+K2/2);
  23.   K4 = h*feval(Fn,tj+h,Zj+K3);
  24.   Z(j+1,:) = Zj + (K1 + 2*K2 + 2*K3 + K4)/6;
  25.   T(j+1) = a + h*j;
  26. end
  27.